<?php

namespace app\modules\api\controllers;

use ac\controllers\BasicController;
use app\models\Experts;
use ac\component\tool\Tool;
use ac\component\exception\ErrMap;

/*专家列表*/
class TeachController extends BasicController
{

    /*列表*/
    public function actionList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $experts = new Experts;
        $count = $experts::find()->count();
        $expertsList = $experts::find()->where('display = 2')
                        ->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        $res = array(
            'list' => $expertsList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        // P($res);
        return Tool::showRes([0, '获取成功'], $res);
    }

	/*详情*/
    public function actionDetail()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $experts = Experts::find()->select(["id", "member_id", "name", "avatar", "goodat", "authenticate_cost", "identified", "introduce", "add_time"])
                        ->where('id = :id and display = 2', [':id' => $id])
                        ->asArray()->one();
            // P($experts);
            if(!is_null($experts)){
                if(!empty($experts['avatar'])){
                    $experts['avatar'] = explode(',', $experts['avatar']);
                }else{
                    $experts['avatar'] = [];
                }
                // P($experts);
                return Tool::showRes([0, '获取成功'], $experts);
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }    

}
